package com.link_with_health.service.impl;

import com.link_with_health.exception.BaseException;
import com.link_with_health.exception.ModifyDatabaseFailedException;
import com.link_with_health.exception.NullQueryResultException;
import com.link_with_health.mapper.AdminDivisionMapper;
import com.link_with_health.mapper.DoctorMapper;
import com.link_with_health.mapper.HospitalMapper;
import com.link_with_health.mapper.HospitalsGradeMapper;
import com.link_with_health.model.AdminDivisionEntity;
import com.link_with_health.model.HospitalEntity;
import com.link_with_health.model.HospitalGradeEntity;
import com.link_with_health.service.BaseService;
import com.link_with_health.service.HospitalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("hospitalService")
public class HospitalServiceImpl extends BaseService implements HospitalService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    HospitalMapper hospitalMapper;
    @Resource
    HospitalsGradeMapper gradeMapper;
    @Resource
    AdminDivisionMapper divisionMapper;
    @Resource
    DoctorMapper doctorMapper;

    /**
     * 通过医院id获取医院信息
     * @param id 医院id
     * @return 医院信息
     * @throws BaseException 异常
     */
    @Override
    public HospitalEntity getHospitalsById(Long id) throws BaseException {
        HospitalEntity entity = null;
        try {
            entity = hospitalMapper.queryHospitalsById(id);
        } catch (Exception e) {
            logger.error("[query Hospital failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("HOSPITAL.HOSPITAL_ID: "+id);
        return entity;
    }

    /**
     * 通过医生id获取医院信息
     * @param id 医生id
     * @return 医院信息
     * @throws BaseException 异常
     */
    @Override
    public HospitalEntity getHospitalsByDoctorId(Long id) throws BaseException {
        HospitalEntity entity = null;
        try {
            entity = hospitalMapper.queryHospitalsByDoctorId(id);
        } catch (Exception e) {
            logger.error("[query hospital failed]",e);
        }
        if(entity == null)
            throw new NullQueryResultException("HOSPITAL JONINt DOCTOR.DR_ID: "+id);

        return entity;
    }

    /**
     * 通过名字获取医院信息 支持模糊查找
     * @param name 医院名字
     * @return 医院信息
     * @throws BaseException 异常
     */
    @Override
    public List<HospitalEntity> getHospitalsByName(String name) throws BaseException {
        List<HospitalEntity> hospitalList = null;
        try {
            hospitalList = hospitalMapper.queryHospitalsByName("%"+name+"%");
        } catch (Exception e) {
            logger.error("[query hospitals failed]",e);
        }
        if (hospitalList == null || hospitalList.isEmpty())
            throw new NullQueryResultException("HOSPITAL.HOSPITAL_NAME: "+name);
        return hospitalList;
    }

    /**
     * 通过地区id获取医院信息 精确到区/县
     * @param divisionId 地区id
     * @return 医院信息
     * @throws BaseException 异常
     */
    @Override
    public List<HospitalEntity> getHospitalsByExactArea(int divisionId) throws BaseException {
        List<HospitalEntity> hospitalList = null;
        try {
            hospitalList = hospitalMapper.queryHospitalsByExactArea(divisionId);
        } catch (Exception e) {
            logger.error("[query hospitals failed]",e);
        }
        if(hospitalList == null || hospitalList.isEmpty())
            throw new NullQueryResultException("HOSPITAL.AREA_ID");

        return hospitalList;
    }

    /**
     * 通过地区id获取医院信息 模糊地区
     * @param divisionId 地区信息
     * @return  医院信息
     * @throws BaseException
     */
    @Override
    public List<HospitalEntity> getHospitalsByWideArea(int divisionId) throws BaseException {
        List<HospitalEntity> hospitalList = null;
        try {
            hospitalList = hospitalMapper.queryHospitalsByWideArea(divisionId);
        } catch (Exception e) {
            logger.error("[query hospitals failed]",e);
        }
        if (hospitalList == null || hospitalList.isEmpty())
            throw new NullQueryResultException("HOSPITAL.AREA_ID: "+divisionId);
        return hospitalList;
    }

    /**
     * 通过医院等级获取医院信息
     * @param grade 等级
     * @return  医院信息
     * @throws BaseException
     */
    @Override
    public List<HospitalEntity> getHospitalsByGrade(int grade) throws BaseException {
        List<HospitalEntity> hospitalList = null;
        try {
            hospitalList = hospitalMapper.queryHospitalsByGrade(grade);
        } catch (Exception e) {
            logger.error("[query hospital failed]",e);
        }
        if(hospitalList == null || hospitalList.isEmpty())
            throw new NullQueryResultException("HOSPITAL.GRADE: "+grade);

        return hospitalList;
    }

    /**
     * 获取所有医院信息
     * @return  医院信息
     * @throws BaseException 异常
     */
    @Override
    public List<HospitalEntity> getAllHospitals() throws BaseException {
        List<HospitalEntity> hospitalList = null;
        try {
            hospitalList = hospitalMapper.queryAllHospitals();
        } catch (Exception e) {
            logger.error("[query hospitals failed]",e);
        }
        if (hospitalList == null || hospitalList.isEmpty())
            throw new NullQueryResultException("ALL HOSPITAL ");
        return hospitalList;
    }

    /**
     *  添加医院信息
     * @param hospital 医院信息
     * @return  改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addHospitals(HospitalEntity hospital) throws BaseException {
        int result = 0;
        try {
            result = hospitalMapper.insertHospitals(hospital);
        } catch (Exception e) {
            logger.error("[add hospital failed]",e);
        }
        if(result == 0)
            throw new ModifyDatabaseFailedException("INSERT HOSPITAL "+hospital);
        return result;
    }

    /**
     * 修改医院信息
     * @param hospital 医院信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int changeHospitals(HospitalEntity hospital) throws BaseException {
        int result = 0;
        try {
            result = hospitalMapper.updateHospitals(hospital);
        } catch (Exception e) {
            logger.error("[change hospital failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE HOSPITAL "+hospital);
        return result;
    }

    /**
     * 删除医院信息
     * @param id 医院id
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int deleteHospitals(Long id) throws BaseException {
        int result = 0;
        try {
            if (doctorMapper.queryDoctorsByHospitalId(id).isEmpty())
                result = hospitalMapper.deleteHospitals(id);
        } catch (Exception e) {
            logger.error("[delete hospital failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("DELETE HOSPITAL: "+id);
        return result;
    }

    /**
     * 通过地区id获取地区信息
     * @param divisionId 地区id
     * @return 地区信息
     * @throws BaseException 异常
     */
    @Override
    public AdminDivisionEntity getAdminDivisionById(int divisionId) throws BaseException {
        AdminDivisionEntity entity = null;
        try {
            entity = divisionMapper.queryAdminDivisionById(divisionId);
        } catch (Exception e) {
            logger.error("[query adminDivision failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("ADMINDIVISION.ID "+divisionId);
        return entity;
    }

    /**
     * 通过省份id获取 市级地区信息
     * @param province 省份id
     * @return  地区信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminDivisionEntity> getAdminDivisionByProvince(Integer province) throws BaseException {
        List<AdminDivisionEntity> divisionList = null;
        try {
            divisionList = divisionMapper.queryAdminDivisionByProvince(province);
        } catch (Exception e) {
            logger.error("[query division failed]",e);
        }
        if (divisionList == null || divisionList.isEmpty())
            throw new NullQueryResultException("ADMINDIVISION.PROVINCE: "+province);
        return divisionList;
    }

    /**
     * 通过城市id获取 区/县级地区信息
     * @param city 城市id
     * @return  地区信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminDivisionEntity> getAdminDivisionByCity(Integer city) throws BaseException {
        List<AdminDivisionEntity> divisionList = null;
        try {
            divisionList = divisionMapper.queryAdminDivisionByCity(city);
        } catch (Exception e) {
            logger.error("[query division failed]",e);
        }
        if(divisionList == null || divisionList.isEmpty())
            throw new NullQueryResultException("ADMINDIVISION.CITY: "+city);
        return divisionList;
    }

    /**
     * 获取所有省级地区信息 含直辖市
     * @return  地区信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminDivisionEntity> getAllProvince() throws BaseException {
        List<AdminDivisionEntity> divisionList = null;
        try {
            divisionList = divisionMapper.queryAllProvince();
        } catch (Exception e) {
            logger.error("[query adminDivision failed]",e);
        }
        if(divisionList == null || divisionList.isEmpty())
            throw new NullQueryResultException("ALL ADMINDIVISION.PROVINCE");
        return divisionList;
    }

    /**
     * 获取所有地区信息
     * @return  地区信息
     * @throws BaseException 异常
     */
    @Override
    public List<AdminDivisionEntity> getAllAdminDivision() throws BaseException {
        List<AdminDivisionEntity> divisionList = null;
        try {
            divisionList = divisionMapper.queryAllAdminDivision();
        } catch (Exception e) {
            logger.error("[query adminDivision failed]",e);
        }
        if(divisionList == null || divisionList.isEmpty())
            throw new NullQueryResultException("ALL ADMINDIVISON ");
        return divisionList;
    }

    /**
     * 通过地区id获取地区完整名字
     * @param divisionId 地区id
     * @return 完整名字
     * @throws BaseException 异常
     */
    @Override
    public String getDivisionName(int divisionId) throws BaseException {
        String divisionName = null;
        try {
            divisionName = divisionMapper.getDivisionName(divisionId);
        } catch (Exception e){
            logger.error("[get division whole name failed]",e);
        }
        if(divisionName == null || "".equals(divisionName))
            throw new NullQueryResultException("ADMINDIVISION.DIVISION_NAME ");
        return divisionName;
    }

    /**
     * 修改地区信息
     * @param adminDivision 地区信息
     * @return 改动数量
     * @throws BaseException
     */
    @Override
    public int changeAdminDivision(AdminDivisionEntity adminDivision) throws BaseException {
        int result = 0;
        try {
            result = divisionMapper.updateAdminDivision(adminDivision);
        } catch (Exception e) {
            logger.error("[change adminDivision failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("UPDATE ADMINDIVISION :" +adminDivision);
        return result;
    }

    /**
     * 添加地区信息
     * @param adminDivision 地区信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addAdminDivision(AdminDivisionEntity adminDivision) throws BaseException {
        int result = 0;
        try {
            result = divisionMapper.insertAdminDivision(adminDivision);
        } catch (Exception e) {
            logger.error("[add adminDivision failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT ADMINDIVISION: "+adminDivision);

        return result;
    }

    /**
     * 删除地区信息
     * @param id 地区id
     * @return  地区信息
     * @throws BaseException 异常
     */
    @Override
    public int deleteAdminDivisionById(int id) throws BaseException {
        int result = 0;
        try {
            result = divisionMapper.deleteAdminDivisionById(id);
        } catch (Exception e) {
            logger.error("[delete adminDivision failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("DELETE ADMINDIVISION.ID : "+id);
        return result;
    }

    /**
     * 通过等级id获取等级信息
     * @param gradeId 等级id
     * @return 等级信息
     * @throws BaseException 异常
     */
    @Override
    public HospitalGradeEntity getGradeById(Integer gradeId) throws BaseException {
        HospitalGradeEntity entity = null;
        try {
            entity = gradeMapper.queryGradeById(gradeId);
        } catch (Exception e) {
            logger.error("[query adminDivision failed]",e);
        }
        if (entity == null)
            throw new NullQueryResultException("ADMINDIVISION.GRADE_ID: "+gradeId);
        return entity;
    }

    /**
     * 通过等级名字获取等级信息
     * @param gradeName 等级名字
     * @return 登记信息
     * @throws BaseException 异常
     */
    @Override
    public List<HospitalGradeEntity> getGradeByName(String gradeName) throws BaseException {
        List<HospitalGradeEntity> gradeList = null;
        try {
            gradeList = gradeMapper.queryGradeByName(gradeName);
        } catch (Exception e) {
            logger.error("[query hospitalGrade failed]",e);
        }
        if (gradeList == null || gradeList.isEmpty())
            throw new NullQueryResultException("HOSPITALGRADE.GRADE_NAME: "+gradeName);

        return gradeList;
    }

    /**
     * 获取所有等级信息
     * @return 等级信息
     * @throws BaseException 异常
     */
    @Override
    public List<HospitalGradeEntity> getAllGrade() throws BaseException {
        List<HospitalGradeEntity> gradeList = null;
        try {
            gradeList = gradeMapper.queryAllGrade();
        } catch(Exception e) {
            logger.error("[query hospitalGrade failed]",e);
        }
        if(gradeList == null || gradeList.isEmpty())
            throw new NullQueryResultException("ALL HOSPITALGRADE");

        return gradeList;
    }

    /**
     * 增加等级信息
     * @param grade 登记信息
     * @return 改动数量
     * @throws BaseException 异常
     */
    @Override
    public int addGrade(HospitalGradeEntity grade) throws BaseException {
        int result = 0;
        try {
            result = gradeMapper.insertGrade(grade);
        } catch (Exception e) {
            logger.error("[add hospitalGrade failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("INSERT HOSPITALGRADE: "+grade);

        return result;
    }

    /**
     * 修改等级信息
     * @param grade 等级信息
     * @return  改动数据
     * @throws BaseException
     */
    @Override
    public int changeGrade(HospitalGradeEntity grade) throws BaseException {
        int result = 0;
        try {
            result = gradeMapper.updateGrade(grade);
        } catch (Exception e) {
            logger.error("[add hospitalGrade failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("");
        return result;
    }

    /**
     * 删除登记信息
     * @param id 等级信息
     * @return 改动数据
     * @throws BaseException
     */
    @Override
    public int deleteGrade(int id) throws BaseException {
        int result = 0;
        try {
            if (hospitalMapper.queryHospitalsByGrade(id).isEmpty())
                result = gradeMapper.deleteGrade(id);
        } catch(Exception e) {
            logger.error("[delete hospitalGrade failed]",e);
        }
        if (result == 0)
            throw new ModifyDatabaseFailedException("DELETE HOSPITALGRADE.ID: "+id);

        return result;
    }
}
